﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace EventScavenger
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (Properties.Settings.Default.NewVersion) //all settings are still default...
            {
                try
                {
                    Properties.Settings.Default.Upgrade();
                    Properties.Settings.Default.NewVersion = false;
                    Properties.Settings.Default.Save();
                }
                catch { }
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool autoLogin = false;
            EventScavengerDAL sqlEventSource = new EventScavengerDAL();

            if (Properties.Settings.Default.SQLServer.Length > 0)
            {
                sqlEventSource.Server = Properties.Settings.Default.SQLServer;
                sqlEventSource.Database = Properties.Settings.Default.Database;
                sqlEventSource.UserName = Properties.Settings.Default.UserName;
                sqlEventSource.Password = Properties.Settings.Default.Password;
                sqlEventSource.SetConnection();
                try
                {
                    sqlEventSource.TestConnection();
                    autoLogin = true;
                }
                catch { }
            }
            if (!autoLogin)
            {
                SetConnection setConnection = new SetConnection();
                if (setConnection.ShowSetConnection() == DialogResult.OK)
                {
                    sqlEventSource.Server = Properties.Settings.Default.SQLServer;
                    sqlEventSource.Database = Properties.Settings.Default.Database;
                    sqlEventSource.UserName = Properties.Settings.Default.UserName;
                    sqlEventSource.Password = Properties.Settings.Default.Password;
                    Properties.Settings.Default.Save();
                }
                else
                {
                    Application.Exit();
                }
            }
            
            Application.Run(new MainForm());
        }
    }
}
